package com.example.java.base.rabbitmq.a5topic;

import com.example.java.base.rabbitmq.util.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Producer:生产消息 动态路由Topic 可以使用通配符
 *
 * @author zhangxiaoxiang
 * @date 2020/11/15
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        String exchangeName="topics";
        // 获取connection
        Connection connection = RabbitMqUtil.getConnection();
        //获取channel
        Channel channel = connection.createChannel();
        //    绑定交换机
        channel.exchangeDeclare(exchangeName, "topic");
        //发送消息
        String routingkey="user.save";
        channel.basicPublish(exchangeName,routingkey,null,("这是topic模型发布的基于routing key: ["+routingkey+"] 发送的消息").getBytes());
        System.out.println("生产了消息...");
        //关闭资源
        RabbitMqUtil.closeConnectionAndChanel(channel, connection);
    }
}
